package com.stormragetech.teamwork.common.util;

import com.stormragetech.common.exception.StormragetechException;
import com.stormragetech.teamwork.common.constant.Constants;

import java.text.SimpleDateFormat;

/**
 * description: //TODO
 *
 * @author: Xue Bin
 * @version: 1.0.0-SNAPSHOT
 */
public class ConvUtils {

    public static Object convert(String source, String type) throws StormragetechException {

        if (null == source || source.length() <= 0) {
            throw new StormragetechException(Constants.CONVERT_ERR_CODE, "数据源参数为空");
        } else if (null == type || type.length() <= 0) {
            throw new StormragetechException(Constants.CONVERT_ERR_CODE, "数据类型参数为空");
        }

        if (Constants.INT_TYPE.equals(type)) {
            return Integer.valueOf(source);
        } else if (Constants.STR_TYPE.equals(type)) {
            return source;
        } else if (Constants.DATE_TYPE.equals(type)) {
            try {
                SimpleDateFormat format = new SimpleDateFormat(Constants.DATE_FORMAT);
                return format.parse(source);
            } catch (Exception e) {
                throw new StormragetechException(Constants.CONVERT_ERR_CODE, "转化日期类型错误", e);
            }
        } else if (Constants.DOUBLE_TYPE.equals(type)) {
            return Double.valueOf(source);
        } else {
            throw new StormragetechException(Constants.CONVERT_ERR_CODE, "不支持的数据类型");
        }

    }

}
